﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace CRM_Web.Controllers
{
    public class LeftMenusController : ApiController
    {
        // GET: api/LeftMenus
        [HttpPost]
        public IHttpActionResult menus([FromBody] Users user)
        {
            using(CRMDBEntities db=new CRMDBEntities())
            {
                //获取登录用户Id
                Users userId = db.Users.SingleOrDefault(p => p.Id == user.Id);
                //根据用户id获取对应用户角色Id
                var rolesid_list = db.UserRoles.Where(p => p.UserId == userId.Id).ToList();
                var list1 = rolesid_list.Select(p => new
                {
                    RoleId = p.RoleId

                }).ToList();
                //用角色id去获得对应模块id
                //父模块
                List<Modules> data1 = new List<Modules>();
                //子模块
                List<Modules> data2 = new List<Modules>();
                List<int> parId = new List<int>();
                foreach (var item in list1)
                {
                    var modulesid_list = db.RoleModules.Where(p => p.RoleId == item.RoleId).ToList();
                    foreach (var i in modulesid_list)
                    {
                        parId.Add(Convert.ToInt32(i.ModuleId));
                    }
                }
                List<int> parentId = parId.Distinct().ToList();
                foreach (var a in parentId)
                {
                    var list7 = db.Modules.OrderByDescending(p => p.Weight).Where(p => p.Id == a).ToList();
                    foreach (var b in list7)
                    {
                        data2.Add(b);
                    }
                }
                //获取子级模块
                var list6 = data2.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).ToList();
                List<int> IDs = new List<int>();
                foreach (var c in list6)
                {
                    IDs.Add(c.ParentId);
                }
                List<int> IDss = IDs.Distinct().ToList();
                foreach (var d in IDss)
                {
                    var list8 = db.Modules.OrderByDescending(p => p.Weight).Where(p => p.Id == d).ToList();
                    foreach (var e in list8)
                    {
                        data1.Add(e);
                    }
                }
                //获取父级模块
                var list5 = data1.Select(p => new
                {
                    Id = p.Id,
                    Name = p.Name,
                    ParentId = p.ParentId,
                    Path = p.Path,
                    Weight = p.Weight
                }).ToList();

                return Json(new { code = 0, data1 = list5, data2 = list6, msg = "请求成功!", username = userId.LoginName });
            }
        }
       
       
    }
}
